
{include file="webautocode@public/header"}

<style>
    .btn{
        width: 200px;
        height: 35px;
        border-radius: 15px;
    }
    .sp-left{
        display: inline-block;
        width: 200px;
        height: 35px;
        line-height: 35px;
        text-align: right;
    }
</style>

<form onsubmit="return false;" data-auto="true" method="post">
    <!--{empty name='$field_list'}-->
    <p class="help-block text-center well">没 有 记 录 哦！</p>
    <!--{else}-->
    <p class="help-block text-center well font-s20">【创建求和接口】</p>
    <div>
        <ul type="disc" style="margin: 20px">
            <listyle="color: red">*多个条件字段则进行and查询</li>
            <li>********************************</li>
            <li style="color: red">可以选择的查询条件：有and查询、like查询、时间范围查询、固定值查询且仅可选择其一</li>
            <li>********************************</li>
            <li>*生成接口时自动生成了接口文档</li>
            <li>*可sum多个字段</li>
            <li style="color: red">*可勾选一个字段进行group by查询</li>
            <li>********************************</li>
        </ul>
    </div>
    <div>
        <input type="text" style="width: 20%" value="{$api_code_root_path}" class="layui-input-block code_save_path" placeholder="代码存放物理路径(默认存放在当前项目的根目录)">
    </div>
    <div style="margin: 30px">
        接口控制器名:
        <input type="text" class="api_controller_name" style="width: 200px;height: 30px" value="{$apiControllerName}">
    </div>
    <div style="margin: 30px">
        <input type="text" class="api_name" style="width: 200px;height: 30px" placeholder="请指定接口名称"
               onblur="ShowFields.saveApiName(this)" value="sumInfo">
        <input type="text" class="api_desc" style="width: 200px;height: 30px" value="{$table_comments}统计"
               placeholder="请说明接口用途">
    </div>
    <div style="margin: 30px">
        <input type="text" class="api_domain" style="width: 200px;height: 30px" placeholder="请指定接口访问域名">
    </div>
    <div style="margin: 30px;color: red">
        温馨提示：单表可多字段求和；分组只能单字段求和，但可以使用聚合函数多字段处理
    </div>
    <table class="layui-table" lay-skin="line">
        <thead>
        <tr>
            <th class='text-left nowrap'></th>
            <th class='text-left nowrap'>[{$tablename}]字段名称</th>
<!--            <th class='text-left nowrap'>响应字段</th>-->
            <th class='text-left nowrap'>and条件字段 <span style="color: red">(必须选择)</span> </th>
            <th class='text-left nowrap'>条件是否必须</th>
            <th class='text-left nowrap'>聚合类型</th>
            <th class='text-left nowrap' style="border-left: 1px solid;">分组字段</th>
            <th class='text-left nowrap' style="border-left: 1px solid;">是否为like查询</th>
            <th class='text-left nowrap'>时间范围条件</th>
            <th class='text-left nowrap'>固定条件</th>
            <!--            <th class='text-left nowrap' style="border-left: 1px solid;">关联表统计</th>-->
        </tr>
        </thead>
        <tbody>
        <!--{foreach $field_list as $key=>$vo}-->
        <tr>
            <td class='list-table-check-td'>
                &nbsp;
            </td>
            <td class='text-left nowrap'>
                {$vo}
            </td>
<!--            <td class='text-left nowrap'>-->
<!--                <input class="list-check-box" name="field_name" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">-->
<!--            </td>-->
            <td class='text-left nowrap'>
                <input class="list-check-box" name="condition_field" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">
            </td>
            <td class='text-left nowrap'>
                <input class="list-check-box" name="is_must" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">
            </td>
            <td class='text-left nowrap'>
<!--                聚合字段<input class="list-check-box" name="aggr_field" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">-->
                聚合函数
                <select name="choose_aggr_func">
                    <option value="">--选择聚合函数--</option>
                    <option value="{$vo}--count">count</option>
                    <option value="{$vo}--max">max</option>
                    <option value="{$vo}--min">min</option>
                    <option value="{$vo}--avg">avg</option>
                    <option value="{$vo}--sum">sum</option>
                </select>
            </td>
            <td class='text-left nowrap' style="border-left: 1px solid;">
                <input class="group_by_field" name="group_by_field" value='{$vo}' type='radio' style="width: 30px;height: 30px;">
            </td>
            <td class='text-left nowrap' style="border-left: 1px solid;">
                like查询<input class="like_field" name="like_field" value='{$vo}' type='checkbox' style="width: 30px;height: 30px;">
            </td>
            <td class='text-left nowrap'>
                时间范围查询<input class="" name="date_scope_field" value='{$vo}' type='checkbox' style="width: 30px;height: 30px">
            </td>
            <td class='text-left nowrap'>
                固定值<input class="" name="fix_field_val" data-field="{$vo}" value='' type='text' style="width: auto;height: 30px">
            </td>
<!--            <td class='text-left nowrap' style="border-left: 1px solid;">-->
<!--                与副表关联-->
<!--                <select name="rel_table" class="rel_table rel_table_{$vo}" onchange="ShowFields.getTableFieldsOptionHtml(this,'{$vo}')">-->
<!--                    <option value="">&#45;&#45;选择关联表&#45;&#45;</option>-->
<!--                    &lt;!&ndash;{foreach $table_list as $key=>$tab_vo}&ndash;&gt;-->
<!--                    <option value="{$vo}-{$tab_vo}" data-tablename="{$tab_vo}">{$vo}-{$tab_vo}</option>-->
<!--                    &lt;!&ndash;{/foreach}&ndash;&gt;-->
<!--                </select>-->
<!--                <select class="rel_field_{$vo}" onchange="ShowFields.groupRelField(this,'{$vo}')">-->
<!--                    <option value="">&#45;&#45;选择关联表字段&#45;&#45;</option>-->

<!--                </select>-->
<!--            </td>-->
        </tr>
        <!--{/foreach}-->
        </tbody>
    </table>
    <div style="margin: 30px">




    </div>
    <div style="margin: 30px" class="api_url">

    </div>
    <div style="margin: 50px">
        <button class="btn" onclick="ShowFields.createsumapi()">生成求和API</button>
    </div>

    <p style="height: 100px">

    </p>
    <!--{/empty}-->
    {include file='public/test_module'}
</form>
<script>
    let ShowFields = {
        _api_name:'sumInfo',
        saveApiName: function (obj){
            this._api_name = $(obj).val();
        },
        //设置接口名称
        setFuncName:function (name){
            if(!this._api_name){
                $('.api_name').val(this._api_name+name);
            }
        },

        //查询表的字段列表
        getTableFieldsOptionHtml: function (obj, main_table_field){
            let url = '{:url("reltablefieldsoptionhtml")}';
            let rel_table_name = $(obj).find('option:selected').attr('data-tablename');
            let data = {
                rel_table_name:rel_table_name
            };
            $.post(url,data,function (res){

                $('.rel_field_'+main_table_field).html(res.data);
            },'json');
        },
        //组装关联表select的值,组装为： id-fa_goods_deal
        groupRelField: function (obj, main_table_field){
            let table = $('.rel_table_'+main_table_field).val();
            $('.rel_table_'+main_table_field).find('option:selected').val(table+"=="+$(obj).val());
        }
        ,
        //执行创建api
        createsumapi: function (){
            let tablename='{$tablename}';
            let url = '{:url("createsumapi")}';
            //查询条件字段
            let condition_field = $('input:checkbox[name="condition_field"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');
            let like_field = $('input:checkbox[name="like_field"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');
            //接口域名
            let api_domain = $('.api_domain').val();
            let api_controller_name = $('.api_controller_name').val();
            if(!api_controller_name){
                return layer.msg('请指定接口控制器名');
            }
            let api_name = $('.api_name').val();
            if(!api_name){
                return layer.msg('请指定接口名');
            }
            //接口描述
            let api_desc = $('.api_desc').val();
            //是否必须
            let is_must = $('input:checkbox[name="is_must"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');

            // let rel_table = $('select[name="rel_table"]').map(function(){
            //     return $(this).val();
            // }).get().join(',');

            let date_scope_field = $('input:checkbox[name="date_scope_field"]:checked').map(function(){
                return $(this).val();
            }).get().join(',');

            //固定字段值
            let fix_field_val = $('input[name="fix_field_val"]').map(function(){
                return $(this).val()?$(this).attr('data-field')+'--'+$(this).val():'';
            }).get().join(',');
            //聚合字段
            let choose_aggr_func = $('select[name="choose_aggr_func"]').map(function(){
                return $(this).val();
            }).get().join(',');
            //分组字段
            let group_by_field = $('.group_by_field:checked').val();

            let data = {
                tablename:tablename,
                condition_field:condition_field,
                like_field:like_field,//like查询字段
                api_name:api_name,//接口名称
                api_desc:api_desc,//接口说明
                //接口控制器名
                api_controller_name:api_controller_name,
                is_must:is_must,//是否必须
                //主表字段
                // main_table_field:$('.main_table_field').text(),
                //关联表（字段-表）
                // rel_table:rel_table,//多个逗号隔开
                api_domain:api_domain,
                date_scope_field:date_scope_field,//时间范围查询
                fix_field_val:fix_field_val,//固定字段值
                code_save_path:$('.code_save_path').val(),//接口代码保存路径
                choose_aggr_func:choose_aggr_func,//聚合函数
                group_by_field:group_by_field,//分组字段
            };
            $.post(url,data,function (res){
                layer.msg(res.msg);
                if(res.code != 200){
                    return ;
                }

                $('.api_url').html(api_domain+res.data.api_url)
            },'json');



        }
    }
</script>
